<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<meta name="generator" content="Doxygen 1.8.13">
<title>OpenCV:图像入门</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon">
<link href="../../tabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css">
<link href="../../stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">图像入门</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>上一个教程:</b> <a class="el" href="../../d6/d15/tutorial_building_tegra_cuda.html">Building OpenCV for Tegra with CUDA</a></p>
<p><b>下一个教程:</b> <a class="el" href="../../d4/db1/tutorial_documentation.html">Writing documentation for OpenCV</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">原作者</td><td align="left">Ana Huamán</td></tr>
<tr>
<td align="right">兼容性</td><td align="left">OpenCV &gt;= 3.4.4 </td></tr>
</table>
<dl class="section warning"><dt>警告</dt><dd>本教程可能包含过时的信息.</dd></dl>
<h2>目标</h2>
<p>在本教程中,您将学习如何:</p>
<ul>
<li>从文件中读取图像(使用<a class="el" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv::imread</a>)</li>
<li>在OpenCV窗口中显示图像(使用<a class="el" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">cv::imshow</a>)</li>
<li>将图像写入文件(使用<a class="el" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv::imwrite</a>)</li>
</ul>
<h2>源代码</h2>
 <div class="newInnerHTML" title="cpp" style="display: none;">C++</div><div class="toggleable_div label_cpp" style="display: none;"><ul>
<li><b>可下载代码</b>:单击<a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/introduction/display_image/display_image.cpp">在这里</a></li>
<li><b>代码一览:</b> <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    std::string image_path = <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(<span class="stringliteral">&quot;starry_night.jpg&quot;</span>);</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(image_path, <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>(img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">    {</div><div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Could not read the image: &quot;</span> &lt;&lt; image_path &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;Display window&quot;</span>, img);</div><div class="line">    <span class="keywordtype">int</span> k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0); <span class="comment">// Wait for a keystroke in the window</span></div><div class="line"><span class="comment"></span></div><div class="line">    <span class="keywordflow">if</span>(k == <span class="charliteral">&#39;s&#39;</span>)</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;starry_night.png&quot;</span>, img);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment -->  </li></ul></div> 

 <div class="newInnerHTML" title="python" style="display: none;">Python</div><div class="toggleable_div label_python" style="display: none;"><ul>
<li><b>可下载代码</b>:单击<a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/introduction/display_image/display_image.py">在这里</a></li>
<li><b>代码一览:</b> <div class="fragment"><div class="line"></div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> sys</div><div class="line"></div><div class="line">img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(<span class="stringliteral">&quot;starry_night.jpg&quot;</span>))</div><div class="line"></div><div class="line"><span class="keywordflow">if</span> img <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    sys.exit(<span class="stringliteral">&quot;Could not read the image.&quot;</span>)</div><div class="line"></div><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&quot;Display window&quot;</span>, img)</div><div class="line">k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(0)</div><div class="line"></div><div class="line"><span class="keywordflow">if</span> k == ord(<span class="stringliteral">&quot;s&quot;</span>):</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&quot;starry_night.png&quot;</span>, img)</div><div class="line"></div></div><!-- fragment -->  </li></ul></div> 

<h2>解释</h2>
 <div class="newInnerHTML" title="cpp" style="display: none;">C++</div><div class="toggleable_div label_cpp" style="display: none;"><p>在opencv3中,我们有多个模块.每一个都负责不同的区域或图像处理方法.您可以在这些教程的用户指南本身的结构中看到这一点.在使用任何模块之前,首先需要包含声明每个模块内容的头文件.</p>
<p>您几乎总是会使用:</p>
<ul>
<li><a class="el" href="../../d0/de1/group__core.html">core</a>核心部分,这里定义了库的基本构建块</li>
<li><a class="el" href="../../d4/da8/group__imgcodecs.html">imgcodecs</a>模块,提供读写功能</li>
<li><a class="el" href="../../d7/dfc/group__highgui.html">highgui</a>模块,因为它包含在窗口中显示图像的函数</li>
</ul>
<p>我们还包括<em>iostream</em>便于控制台行输出和输入.</p>
<p>宣布<code>using namespace cv;</code>,在下面的示例中,可以访问库函数,而无需显式说明名称空间.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div></div><!-- fragment --> </div>  <div class="newInnerHTML" title="python" style="display: none;">Python</div><div class="toggleable_div label_python" style="display: none;"><p>第一步,导入 OpenCV python 库.正确的方法是为它另外指定一个名称<em>cv</em>,用于以下引用库.</p>
<div class="fragment"><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> sys</div></div><!-- fragment --> </div> <p>现在,让我们分析一下主代码.作为第一步,我们从OpenCV示例中读取图像 "starry_night.jpg" .为了做到这一点,调用<a class="el" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv::imread</a>函数使用第一个参数指定的文件路径加载图像.第二个参数是可选的,用于指定图像的格式.这可能是:</p>
<ul>
<li>IMREAD_COLOR 以 BGR 8-bit 格式加载图像.这就是<b>默认</b>这里用的.</li>
<li>IMREAD_UNCHANGED 按原样加载图像(包括alpha通道(如果存在))</li>
<li>IMREAD_GRAYSCALE 灰度将图像作为强度加载</li>
</ul>
<p>读入图像后,数据将存储在<a class="el" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a>对象.</p>
 <div class="newInnerHTML" title="cpp" style="display: none;">C++</div><div class="toggleable_div label_cpp" style="display: none;"> <div class="fragment"><div class="line">    std::string image_path = <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(<span class="stringliteral">&quot;starry_night.jpg&quot;</span>);</div><div class="line">    Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(image_path, <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div></div><!-- fragment --> </div>  <div class="newInnerHTML" title="python" style="display: none;">Python</div><div class="toggleable_div label_python" style="display: none;"> <div class="fragment"><div class="line">img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(<span class="stringliteral">&quot;starry_night.jpg&quot;</span>))</div></div><!-- fragment --> </div> <dl class="section note"><dt>Note</dt><dd>OpenCV支持Windows位图(bmp)、可移植图像格式(pbm、pgm、ppm)和Sun光栅(sr、ras)等图像格式.在插件的帮助下(如果您自己构建库,您需要指定使用它们,但是在我们默认提供的软件包中),您还可以加载JPEG(JPEG,jpg,jpe)、JPEG 2000(jp2-在CMake中代号为Jasper)、TIFF文件(TIFF,tif)和便携式网络图形(png)等图像格式.此外,OpenEXR也是一种可能性.</dd></dl>
<p>之后,如果图像加载正确,则执行检查.</p> <div class="newInnerHTML" title="cpp" style="display: none;">C++</div><div class="toggleable_div label_cpp" style="display: none;"> <div class="fragment"><div class="line">    <span class="keywordflow">if</span>(img.empty())</div><div class="line">    {</div><div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Could not read the image: &quot;</span> &lt;&lt; image_path &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class="newInnerHTML" title="python" style="display: none;">Python</div><div class="toggleable_div label_python" style="display: none;"> <div class="fragment"><div class="line"><span class="keywordflow">if</span> img <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    sys.exit(<span class="stringliteral">&quot;Could not read the image.&quot;</span>)</div></div><!-- fragment --> </div> <p>然后,通过调用<a class="el" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">cv::imshow</a>功能.第一个参数是窗口的标题,第二个参数是<a class="el" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a>将显示的对象.</p>
<p>因为我们希望在用户按下某个键之前显示窗口(否则程序结束得太快),所以我们使用<a class="el" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv::waitKey</a>函数的唯一参数是它应该等待用户输入多长时间(以毫秒为单位).零意味着永远等待.返回值是按下的键.</p>
 <div class="newInnerHTML" title="cpp" style="display: none;">C++</div><div class="toggleable_div label_cpp" style="display: none;"> <div class="fragment"><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;Display window&quot;</span>, img);</div><div class="line">    <span class="keywordtype">int</span> k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0); <span class="comment">// Wait for a keystroke in the window</span></div></div><!-- fragment --> </div>  <div class="newInnerHTML" title="python" style="display: none;">Python</div><div class="toggleable_div label_python" style="display: none;"> <div class="fragment"><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&quot;Display window&quot;</span>, img)</div><div class="line">k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(0)</div></div><!-- fragment --> </div> <p>最后,如果按下的键是“s”键,图像将被写入文件.为了这个<a class="el" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce" title="Saves an image to a specified file. ">cv::imwrite</a>调用的函数具有文件路径和<a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">cv::Mat</a>对象作为参数.</p>
 <div class="newInnerHTML" title="cpp" style="display: none;">C++</div><div class="toggleable_div label_cpp" style="display: none;"> <div class="fragment"><div class="line">    <span class="keywordflow">if</span>(k == <span class="charliteral">&#39;s&#39;</span>)</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;starry_night.png&quot;</span>, img);</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class="newInnerHTML" title="python" style="display: none;">Python</div><div class="toggleable_div label_python" style="display: none;"> <div class="fragment"><div class="line"><span class="keywordflow">if</span> k == ord(<span class="stringliteral">&quot;s&quot;</span>):</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&quot;starry_night.png&quot;</span>, img)</div></div><!-- fragment --> </div>  </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"><address class="footer"><small>Generated on Fri Apr 2 2021 11:36:36 for OpenCV by<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen">
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
